#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        WaitforElementById(driver,"com.bbk.appstore:id/download_entry",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    time.sleep(1)
    while True:
        try:
            btns=WaitforElementsById(driver,"com.bbk.appstore:id/cancel_btn_layout",30)
        except NoSuchElementException:
            break
        else:
            btns[0].click()
            time.sleep(2)
            try:
                WaitforElementById(driver,"com.bbk.appstore:id/common_dialog_positive_btn",30).click()
            except NoSuchElementException:
                continue
        time.sleep(1)
    driver.back()


def clickappdown(driver):
    try:
        tasks=WaitforElementsById(driver,'com.bbk.appstore:id/package_list_item_layout',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in tasks:
            try:
                size=WaitforElementById(btn,"com.bbk.appstore:id/package_list_item_size_and_download_counts",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size.split()[0][0:-1])<20:
                    continue
            try:
                down_btn=WaitforElementById(btn,'com.bbk.appstore:id/download_layout',30)
            except NoSuchElementException:
                continue
            else:
                down_btn.click()
                waittime=40
                while waittime:
                    text=WaitforElementById(btn,'com.bbk.appstore:id/download_size_info_tv',5).get_attribute("text")
                    print text
                    if '0.00' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"

def game(driver):
    try:
        WaitforElementByName(driver,u"游戏",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'休闲益智',u'动作冒险',u'赛车体育',u'飞行射击',u'经营策略',u'棋牌天地',u'角色扮演']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    #driver.back()

def soft(driver):
    try:
        WaitforElementByName(driver,u"应用",30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'视频音乐',u'通讯社交',u'摄影美图',u'新闻阅读',u'购物优惠',u'生活服务', \
                   u'实用工具',u'教育学习',u'系统工具',u'金融理财',u'旅游出行',u'娱乐消遣',  \
                   u'育儿母婴',u'健康美容',u'效率办公']
    #gamecatetory= [u'视频音乐']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                time.sleep(2)
                driver.back()
            except NoSuchElementException:
                time.sleep(2)
                driver.back()
                continue
    #driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE        # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.bbk.appstore'    # apk的包名
    desired_caps['appActivity'] = '.ui.AppStore'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()
